package com.zhxxfwzx.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhxxfwzx.common.lang.RespBean;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description TODO
 * @title: MyLogoutSuccessHandler
 * @Author LiJing
 * @Date: 2021/6/161:22 下午
 * @Version 1.0
 */
@Component
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        response.setContentType("application/json;charset=uft-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        PrintWriter writer = response.getWriter();
        RespBean res = RespBean.ok("logout success!");
        writer.write(new ObjectMapper().writeValueAsString(res));
        writer.flush();
        writer.close();
    }
}
